package com.sunwayworld.baseline.framework.utils;

import com.sunwayworld.framework.utils.ObjectUtils;
import jcifs.smb.SmbFile;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class SmbV1Utils {

    private static final Logger logger = LogManager.getLogger(SmbV1Utils.class);

    public static boolean connect(String host, String username, String password){
        StringBuffer url = new StringBuffer("smb://");
        if (!ObjectUtils.isEmpty(username)){
            url = url.append(username).append(":");
        }
        if (!ObjectUtils.isEmpty(password)){
            url = url.append(password).append("@");
        }
        url = url.append(host);
        boolean result = true;
        try{
            SmbFile smbFile = new SmbFile(url.toString());
            smbFile.connect();
        }catch (Exception e){
            e.printStackTrace();
            result = false;
        }
        return result;
    }

    public static SmbFile getSmbFile(String path, String host, String username, String password){
        StringBuffer url = new StringBuffer("smb://");
        if (!ObjectUtils.isEmpty(username)){
            url = url.append(username).append(":");
        }
        if (!ObjectUtils.isEmpty(password)){
            url = url.append(password).append("@");
        }
        url = url.append(host).append(path);
        SmbFile smbFile;
        try{
            smbFile = new SmbFile(url.toString());
            smbFile.connect();
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("GIKAM.EXCEPTION.SMB.CONNECTION");
        }
        return smbFile;
    }

}
